﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Security.Principal;
using System.Reflection;
using System.Diagnostics;
using Nexus.IPConfiguration.Management.Wrappers;
using Nexus.IPConfiguration.Configurator;
using Nexus.IPConfiguration.Configurator.Properties;
using System.Threading;
using System.Globalization;
using Microsoft.Win32;

namespace Configurator
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static int Main(string[] args)
        {
            RegistryKey key;
            while ((key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\IpConfigurator", true)) == null)
            {
                Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("IpConfigurator");
            }
            string locale = key.GetValue("Locale") as string;
            if (string.IsNullOrWhiteSpace(locale))
            {
                locale = "en-gb";
                key.SetValue("Locale", locale);
            }
            if (locale.ToLower() == "en-gb")
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-gb");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-gb");
            }
            else
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("pl-pl");
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("pl-pl");
            }
            key.Dispose();
            int result = 0;
            if (args.Length > 0)
            {
                if (args.Length == 12)
                {
                    string adapterName;
                    IPConfig ipConfig;
                    DNS dns;
                    ProxyConfig proxyConfig;
                    ConfigChanger.DecodeParams(args, out adapterName, out ipConfig, out dns, out proxyConfig);
                    ConfigChanger.ChangeConfig(adapterName, ipConfig, dns, proxyConfig);
                }
                else
                {
                    //TODO - handle
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
            return result;
        }


        private static bool IsRunningUACElevated
        {
            get
            {
                WindowsIdentity wi = WindowsIdentity.GetCurrent();
                WindowsPrincipal wp = new WindowsPrincipal(wi);

                return wp.IsInRole(WindowsBuiltInRole.Administrator);
            }
        }
    }
}
